package com.jinsp.webviewservice.webview;

import android.graphics.Bitmap;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by luoshuai on 12/30/21
 * <p>
 * 接收HTML加载状态
 */
public class DefaultWebViewClient extends WebViewClient {

    private final IWebViewCallback webViewCallback;

    public DefaultWebViewClient(IWebViewCallback webViewCallback) {
        this.webViewCallback = webViewCallback;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if (webViewCallback != null) {
            //开始加载HTML
            webViewCallback.startLoading(url);
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        if (webViewCallback != null) {
            //加载完毕
            webViewCallback.finishLoading(url);
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        if (webViewCallback != null) {
            //加载错误
            webViewCallback.loadError(errorCode, description);
        }
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        //允许内部跳转
        view.loadUrl(url);
        return true;
    }
}
